﻿namespace WLFramework.Validation
{
    using System;
    using WLFramework.Validation;

    [AttributeUsage(AttributeTargets.Property)]
    public class CustomRegularAttribute : ValidationAttribute
    {
        public string RegexText { get; set; }

        public CustomRegularAttribute(string regexText,string displayErrors)
            : this(regexText,displayErrors, "请输入{0}")
        {
        }

        public CustomRegularAttribute(string regexText,string displayErrors, string displayDefault)
        {
            this.RegexText = regexText;
            this.ErrorMessage = displayErrors;
            this.DefaultMessage = displayDefault;
        }

        public override ValidatePriority Priority
        {
            get
            {
                return ValidatePriority.High;
            }
        }

        public override ValidateResult IsValid(object input, ValidateContext context)
        {
            return RegexValidation.IsCustom(input.ToString(), this.RegexText) ?
                ValidateResult.Success :
                new ValidateResult(this.FormatErrorMessage(context.DisplayName));
        }
    }
}
